﻿//********************************************************************************//
//创建日期：<创建日期，2016-02-26>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：行政区信息
//********************************************************************************//
using System;
using System.Web.UI;
using Entity;
using BLL;
using System.Data;
using Web.Class;

namespace Web.Pages.YongShui.JiChuXX
{
    public partial class XingZhengQXX : BasePage
    {
        #region 属性

        /// <summary>
        /// 当前操作项ID
        /// </summary>
        private int XingZhengQID
        {
            get
            {
                return Convert.ToInt32(ViewState["_XingZhengQID"]);
            }
            set
            {
                ViewState["_XingZhengQID"] = value;
            }
        }
        /// <summary>
        /// 保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 行政区对象
        /// </summary>
        YS_JX_XINGZHENGQ _YS_JX_XINGZHENGQ = new YS_JX_XINGZHENGQ();

        /// <summary>
        /// 行政区BLL
        /// </summary>
        YS_JX_XINGZHENGQBLL _YS_JX_XINGZHENGQBLL = new YS_JX_XINGZHENGQBLL();

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitData();   
            }
            LoadData();
        }

        /// <summary>
        /// 绑定空表头
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {
            base.Render(sender);
        }

       

        /// <summary>
        /// 响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            CallbackResult cr = new CallbackResult(e.Parameter);
            int id = Convert.ToInt32(this.hdCollection.Get("ID"));
            if (e.Parameter == "info")
            {      
                this._YS_JX_XINGZHENGQ = this._YS_JX_XINGZHENGQBLL.SelectYS_JX_XINGZHENGQ(id);
                cr.IsSuccess = true;
                cr.IsAlertMsg = false;
                cr.AddValue(this._YS_JX_XINGZHENGQ.S_BIANHAO,this._YS_JX_XINGZHENGQ.S_MINGCHENG,this._YS_JX_XINGZHENGQ.S_MIAOSHU);
                e.Result = cr.ToString();
                return;
            }
            else if (e.Parameter == "del")
            {
                _YS_JX_XINGZHENGQ = _YS_JX_XINGZHENGQBLL.SelectYS_JX_XINGZHENGQ(id);
                //判断行政区是否正在使用,如使用不允许删除
                if (new YS_YG_YONGHUXXBLL().SelectYS_YG_YONGHUXXesDynamic(string.Format("s_xingzhengqbh='{0}'", _YS_JX_XINGZHENGQ.S_BIANHAO), null).Rows.Count > 0)
                {
                    cr.IsSuccess = false;
                    cr.IsAlertMsg = true;
                    cr.Msg= "本行政区正在使用中,不允许删除！";
                    e.Result = cr.ToString();
                    return;
                }
                //执行逻辑删除            
                _YS_JX_XINGZHENGQ.S_CAOZUOR = LoginInfo.CurrentUser.Account;
                _YS_JX_XINGZHENGQ.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
                _YS_JX_XINGZHENGQ.I_JLZT = -1;
                _SYS_LOGBLL.Log<YS_JX_XINGZHENGQ>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "行政区信息", OperateState.Delete, _YS_JX_XINGZHENGQ, null);
                if (_YS_JX_XINGZHENGQBLL.InsertOrUpdate(_YS_JX_XINGZHENGQ))
                {
                    
                    cr.IsSuccess = true;
                    cr.IsAlertMsg = true;
                    cr.Msg = "操作成功！";
                    e.Result = cr.ToString();
                    return;
                }
            }
            else if (e.Parameter == "save")
            {
                OperateState operateState;
                //添加
                if (id==-1)
                {
                    _YS_JX_XINGZHENGQ.ID = 0;
                    _YS_JX_XINGZHENGQ.I_JLZT = 0;
                    operateState = OperateState.Add;
                }
                //修改
                else
                {
                    //修改操作
                    _YS_JX_XINGZHENGQ = _YS_JX_XINGZHENGQBLL.SelectYS_JX_XINGZHENGQ(id);
                    operateState = OperateState.Modify;
                }
                //判断是否编号已存在
                if (_YS_JX_XINGZHENGQBLL.SelectYS_JX_XINGZHENGQsDynamic(string.Format("I_JLZT=0 and S_BIANHAO='{0}' and ID<>'{1}'", this.txtBianHao.Text, this._YS_JX_XINGZHENGQ.ID), null).Rows.Count > 0)
                {
                    cr.IsSuccess = false;
                    cr.IsAlertMsg = true;
                    cr.Msg = "您所输入的编号,系统中已经存在使用,请修改。！";
                    e.Result = cr.ToString();
                    return;
                }
                _YS_JX_XINGZHENGQ.S_BIANHAO = txtBianHao.Text;
                _YS_JX_XINGZHENGQ.S_MINGCHENG = txtMingCheng.Text;
                _YS_JX_XINGZHENGQ.S_MIAOSHU = memoMiaoShu.Text;
                _YS_JX_XINGZHENGQ.I_JLZT = 0;
                this._YS_JX_XINGZHENGQ.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                this._YS_JX_XINGZHENGQ.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
                _SYS_LOGBLL.Log<YS_JX_XINGZHENGQ>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "行政区信息", operateState, _YS_JX_XINGZHENGQ, null);
                if (_YS_JX_XINGZHENGQBLL.InsertOrUpdate(_YS_JX_XINGZHENGQ))
                {
                    
                    cr.IsSuccess = true;
                    cr.IsAlertMsg = true;
                    cr.Msg="保存成功！";
                    cr.SpecialIndetity=_YS_JX_XINGZHENGQ.ID.ToString();
                    e.Result = cr.ToString();
                    return;
                }
            }
            
        }

        /// <summary>
        /// 客户端刷新list
        /// </summary>
        protected void gvList_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadData();
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            txtBianHao.Text = txtMingCheng.Text = memoMiaoShu.Text = "";
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            this.dtSource = _YS_JX_XINGZHENGQBLL.SelectYS_JX_XINGZHENGQsAll();
            gvXingZhengQ.DataSource = this.dtSource;
            gvXingZhengQ.DataBind();
        }

        #endregion        
    }
}